using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Resources;
using System.Text;
using Eclipse.TRCore.Properties;

namespace Eclipse.TRCore.Travians.Servers
{
    public static class ServerDef
    {
        public readonly static IEnumerable<string> ServerUrls;
        private static IDictionary<string, IEnumerable<string>> texts;
        static ServerDef()
        {
            ServerUrls = Init();
        }

        public static IEnumerable<string> GetCountrys()
        {
            return texts.Keys;
        }

        public static string GetCountry(string url)
        {
            foreach (KeyValuePair<string, IEnumerable<string>> pair in texts)
            {
                foreach (string str in pair.Value)
                {
                    if (str.Equals(url, StringComparison.InvariantCultureIgnoreCase))
                        return pair.Key;
                }
            }
            return null;
        }

        public static IEnumerable<string> GetUrls(string country)
        {
            return texts[country];
        }

        static IEnumerable<string> Init()
        {
            texts = new Dictionary<string, IEnumerable<string>>();
            using (StringReader reader = new StringReader(Resources.Urls))
            {
                string lastCountry = null;
                while (true)
                {
                    string line = reader.ReadLine();
                    if (null == line)
                        break;
                    line = line.Trim();
                    if (line.Length == 0 || line.StartsWith("//"))
                        continue;
                    if (!line.StartsWith("*"))
                    {
                        line = Path.Combine("http://", line);
                        ((List<string>)texts[lastCountry]).Add(line);
                        yield return line;
                    }
                    else
                    {
                        texts.Add(lastCountry = line.Substring(1), new List<string>());
                    }

                }
            }
        }
    }
}
